package com.tsinghuabigdata.edu.ddmath.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.constant.AppConst;
import com.tsinghuabigdata.edu.ddmath.util.AppLog;
import com.tsinghuabigdata.edu.ddmath.util.AppUtils;

/**
 * <p>
 * Created by yanshen@tsinghuabigdata.com on 2015/12/17.
 * </p>
 * <p/>
 * 网络加载故障提示界面
 *
 * @author yanshen@tsinghuabigdata.com
 * @version V1.0
 * @packageName: com.tsinghuabigdata.edu.ddmath.controls
 * @createTime: 2015/12/17 14:13
 */
public class LoadDataFaultView extends RelativeLayout implements View.OnClickListener {

    //
    private ImageView nowifi_btn;
    private TextView nolink_btn;
    private TextView notips_btn;
    private TextView no_data_txt;
    private Button   tryBtn;
    private LinearLayout no_data_llcontainer;
    private LinearLayout fault_llcontainer;

    private View mainView;

    private boolean mNoData;
    private String noDataMessage = "什么数据都没有发现";

    //异常错误的原因
    protected int exception_cause = 0;


    private OnClickListener mListener;

    public LoadDataFaultView(Context context) {
        super(context);
        initialize();
    }

    public LoadDataFaultView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public LoadDataFaultView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    /**
     * 初始化界面元素
     */
    private void initialize() {
        mainView = inflate(getContext(), R.layout.layout_load_data_fault, this);

        nowifi_btn = (ImageView) findViewById(R.id.nowifi_btn);
        nolink_btn = (TextView) findViewById(R.id.nolink_btn);
        notips_btn = (TextView) findViewById(R.id.notips_btn);
        no_data_txt = (TextView) findViewById(R.id.no_data_txt);
        no_data_llcontainer = (LinearLayout) findViewById(R.id.nodata_layout);
        fault_llcontainer = (LinearLayout) findViewById(R.id.fault_layout);
        tryBtn = (Button)findViewById( R.id.no_data_trybtn );

        findViewById(R.id.nowifi_btn).setOnClickListener(this);
        findViewById(R.id.nolink_btn).setOnClickListener(this);
        findViewById(R.id.notips_btn).setOnClickListener(this);
        no_data_llcontainer.setOnClickListener(this);
        tryBtn.setOnClickListener(this);
        setOnClickListener(this);
    }

    public void show() {
        setVisibility(VISIBLE);
        invalidateDisplay();
    }

    public void hidden() {
        setVisibility(GONE);
    }

    public void invalidateDisplay() {
        if (mNoData) {
            no_data_txt.setText(noDataMessage);
            findViewById(R.id.no_data_image).setVisibility(GONE);
            findViewById(R.id.nodata_layout).setVisibility(VISIBLE);
            findViewById(R.id.fault_layout).setVisibility(GONE);
        } else {
            if (AppUtils.isNoNetwork() || AppConst.CONNECT_TIMEOUT == exception_cause ) {   //无网
                //nowifi_btn.setImageResource(R.drawable.icon_nowifi);
                nolink_btn.setText("无法连接到网络");
                notips_btn.setText("请检查您的网络后点击此区域重试");

                findViewById(R.id.nodata_layout).setVisibility(GONE);
                findViewById(R.id.fault_layout).setVisibility(VISIBLE);

            } else if( !noDataMessage.equals("什么数据都没有发现") ){                        //无数据

                findViewById(R.id.no_data_image).setVisibility(GONE);
                no_data_txt.setText(noDataMessage);

                findViewById(R.id.nodata_layout).setVisibility(VISIBLE);
                findViewById(R.id.fault_layout).setVisibility(GONE);
            } else {                                                                        //接口异常
                nolink_btn.setText(getResources().getString( R.string.server_exception ));
                notips_btn.setText("请点击界面重试");

                findViewById(R.id.nodata_layout).setVisibility(GONE);
                findViewById(R.id.fault_layout).setVisibility(VISIBLE);
            }
        }
    }

    public void setFaultBackgroundColor(int color){
        mainView.setBackgroundColor( color );
    }

    /**
     * 绑定点击事件
     *
     * @param listener
     */
    public void setListener(OnClickListener listener) {
        this.mListener = listener;
    }


    @Override
    public void onClick(View v) {
        if (mListener != null) {
            mListener.onClick(v);
        }
    }

    public void setNoData(boolean noData) {
        this.mNoData = noData;
    }

    public void setNoDataMessage(String noDataMessage) {
        this.noDataMessage = noDataMessage;
    }
}
